<?php

/**
 * 综合搜索
 */
class SearchAction extends HomeAction {
    
    public function _initialize() {
        parent::_initialize();
        $this->seo('综合搜索 ');
    }

    public function index() {
        //@todo 分页搜索有效
        $type    = t($_POST['type']) ? t($_POST['type']) : 'gezi';
        $keyword = ($_POST['keyword'] == '请输入关键字')? '': t($_POST['keyword']);
        $uname = ($_POST['uname'] == '请输入关键字')? '': t($_POST['uname']);
        $cat_one = t($_POST['cate_one']);
        $cat_two = t($_POST['cate_two']);
        $map     = array();
        if ($uname) {
            $where['uname'] = array('LIKE', '%' . $uname . '%');
            $uids           = M('User')->where($where)->select();
            $map['user_id'] = array('IN', getSubByKey($uids, 'user_id'));
        }
        switch ($type) {
            case 'gezi':
                // <editor-fold defaultstate="collapsed" desc="格子">
                $stitle = '格子';
                if ($keyword) {
                    $map['title'] = array('LIKE', '%' . $keyword . '%');
                }
                $map['status_check'] = 'true';
                $mod   = M('Gezi');
                //取得满足条件的记录数
                $count = $mod->where($map)->count($mod->getPk());
                if ($count > 0) {
                    import("ORG.Util.Page");
                    $order = 'create_time DESC';
                    $limit = 20;
                    //创建分页对象
                    $p     = new Page($count, $limit);
                    //分页查询数据

                    $voList = $mod->where($map)->order($order)->limit($p->firstRow . ',' . $p->listRows)->select();
                    //分页显示
                    $page   = $p->show();
                    //模板赋值显示
                    $data   = array(
                        'list'      => $voList,
                        'page'      => $page,
                        'nowPage'   => $_REQUEST['page'],
                        'totalPage' => round($count / $limit),
                    );
                }// </editor-fold>
                break;
            case 'catad':
                // <editor-fold defaultstate="collapsed" desc="分类广告">
                $stitle     = '分类广告';
                if ($keyword) {
                    $map['title'] = array('LIKE', '%' . $keyword . '%');
                }
                $map['status_check'] = 'true';
                //二级分类
                if ($cat_two) {
                    $map['cat_id'] = $cat_two;
                }
                if ($cat_one && $cat_two == 0) {
                    $cat = D('Cat')
                            ->field("*,cat_id as id,parent_id as parentid")
                            ->where(array('parent_id' => $cat_one))
                            ->order(array("sort_order"   => "ASC"))
                            ->select();
                    $map['cat_id'] = array("IN", getSubByKey($cat, 'cat_id'));
                }
                $mod   = M('Catad');
                //取得满足条件的记录数
                $count = $mod->where($map)->count($mod->getPk());
                if ($count > 0) {
                    import("ORG.Util.Page");
                    $order = 'create_time DESC';
                    $limit = 20;
                    //创建分页对象
                    $p     = new Page($count, $limit);
                    //分页查询数据

                    $voList = $mod->where($map)->order($order)->limit($p->firstRow . ',' . $p->listRows)->select();
                    //分页显示
                    $page   = $p->show();
                    //模板赋值显示
                    $data   = array(
                        'list'      => $voList,
                        'page'      => $page,
                        'nowPage'   => $_REQUEST['page'],
                        'totalPage' => round($count / $limit),
                    );
                }// </editor-fold>
                break;
            case 'service':
                //分类
                $stitle     = '生活服务';

                $mod = M('Service');
                $map = array();
                if ($cat_one) {
                    $map['cat_ids'] = $cat_one;
                }

                if ($keyword) {
                    $map['name'] = array('LIKE', '%' . $keyword . '%');
                }
                $order = ' create_time DESC ';
                $limit = 10;
                //取得满足条件的记录数
                $count = $mod->where($map)->count($mod->getPk());
                if ($count > 0) {
                    import("ORG.Util.Page");
                    //创建分页对象
                    $p      = new Page($count, $limit);
                    //分页查询数据
                    $voList = $mod
                            ->join('AS s JOIN ' . C('DB_PREFIX') . 'service_cat as c ON c.cat_id=s.cat_ids ')
                            ->join(C('DB_PREFIX') . 'card_theme as ct ON s.theme_id=ct.theme_id')
                            ->where($map)
                            ->order($order)
                            ->field('s.*, c.title as cat_name, ct.*')
                            ->limit($p->firstRow . ',' . $p->listRows)
                            ->select();
                    //分页显示
                    $page   = $p->show();
                    //模板赋值显示
                    $data   = array(
                        'list'      => $voList,
                        'page'      => $page,
                        'nowPage'   => $_REQUEST['page'],
                        'totalPage' => round($count / $limit),
                    );
                }
                break;
            case 'dazhe':
                // <editor-fold defaultstate="collapsed" desc="打折优惠">
                $stitle     = '打折优惠';
                $dazhemod   = M('Dazhe');
                $order      = ' create_time DESC ';
                $limit      = 20;


                //分类置顶|首页置顶
                if ($cat_one) {
                    $map['d.cat_id'] = $cat_one;
                    $order .= ',cat_top_status DESC ';
                } else {
                    $order .= ',index_top_status DESC ';
                }

                if ($keyword) {
                    $map['d.title'] = array('LIKE', '%' . $keyword . '%');
                }
                //取得满足条件的记录数
                $count = $dazhemod
                        ->join('AS d JOIN ' . C('DB_PREFIX') . 'cat as c ON c.cat_id=d.cat_id ')
                        ->where($map)
                        ->count($dazhemod->getPk());

                if ($count > 0) {
                    import("ORG.Util.Page");
                    //创建分页对象
                    $p = new Page($count, $limit);
                    //分页查询数据

                    $voList = $dazhemod
                            ->join('AS d JOIN ' . C('DB_PREFIX') . 'cat as c ON c.cat_id=d.cat_id ')
                            ->where($map)
                            ->order($order)
                            ->field('d.*, c.name as cat_name')
                            ->limit($p->firstRow . ',' . $p->listRows)
                            ->select();
                    //分页显示
                    $page   = $p->show();
                    //模板赋值显示
                    $data   = array(
                        'list'      => $voList,
                        'page'      => $page,
                        'nowPage'   => $_REQUEST['page'],
                        'totalPage' => round($count / $limit),
                    );
                }// </editor-fold>

                break;
        }

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->assign('stitle', $stitle);
        $this->assign('type', $type);
        $this->assign('data', $data);
        $this->display();
    }

    public function ajax() {
        $do     = t($_REQUEST['do']);
        $reData = reStatus();
        switch ($do) {
            case 'cat':
                $type = t($_REQUEST['type']);
                switch ($type) {
                    case 'gezi':
                        break;
                    case 'catad':
                        //分类
                        $cat = D('Cat')
                                ->field("*,cat_id as id,parent_id as parentid")
                                ->where(array('parent_id' => 0))
                                ->order(array("sort_order" => "ASC"))
                                ->select();
                        break;
                    case 'service':
                        //分类
                        $cat         = D('ServiceCat')
                                ->where(array('status' => 1))
                                ->select();
                        foreach ($cat as $ck => $cv) {
                            $cat[$ck]['name'] = $cv['title'];
                        }
                        break;
                    case 'dazhe':
                        //分类
                        $cat              = D('Cat')
                                ->field("*,cat_id as id,parent_id as parentid")
                                ->where(array('parent_id' => 0))
                                ->order(array("sort_order" => "ASC"))
                                ->select();
                        break;
                    case 'child':
                        $topType     = t($_REQUEST['tp']);
                        $parentId    = format_int($_REQUEST['pid']);
                        if ($topType == 'catad') {
                            $cat = D('Cat')
                                    ->field("*,cat_id as id,parent_id as parentid")
                                    ->where(array('parent_id' => $parentId))
                                    ->order(array("sort_order" => "ASC"))
                                    ->select();
                        }
                        break;
                }
                $reData      = reStatus($cat ? true : false, '', $cat);
                break;
        }
        $this->ajaxReturn($reData);
    }

}
